#include <iostream>
#include <math.h>
#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

void readBlock(IplImage* image, int row, int column);

int main( int argc, char* argv[]) {

int row, column;

if(argc != 2) 
{ 
   cout <<" The command is not right! " << endl;
   return -1;
}

//load a color image
IplImage* image = cvLoadImage(argv[1], 1);

cout << "*************************************" <<endl <<endl;
cout << "Width is: " << image->width <<"; " << "Height is: " << image->height << endl;
cout << "Depth is: " << image->depth <<"; " << "Widthstep is: "<< image->widthStep << endl;
cout << "nChannel is: "<< image->nChannels <<"; "<<"Imagesize is: "<< image->imageSize << endl;
cout << "Origin is: " <<image->origin <<"; "<<"Dataorder is: "<< image->dataOrder <<endl;
cout << "nSize is: " <<image->nSize <<"; " <<"ID is: " << image->ID << endl;
cout << "Align is: " <<image->align <<"; " <<"alphaChannel is: "<< image->alphaChannel <<endl;
cout << endl << "*************************************" <<endl;

do {
	cout << "Please input the row: " << endl;
	cin >> row;
	cout << "Please input the column: " << endl;
	cin >> column;
   }while (row >= (image->height - 1) | row < 1 | column >= (image->width - 1)|column<1);
  
readBlock(image, row, column);

}


void readBlock(IplImage* image, int row, int column) {

int i, j;
int channel;
int pix[3];
uchar* thisrow;

cout << "The 3x3 blocks of pixel of centered by" << "("<<row << "," << column <<")"<<"are: "<< endl;

for( i = 0; i < 3; i ++) {
  
  thisrow = (uchar *)(image->imageData + (row+i-1) * (image->widthStep));
  
  for( j = 0; j < 3; j++) {
    
          pix[0] = thisrow[3*(column+j-1)];
          pix[1] = thisrow[3*(column+j-1)+1];
          pix[2] = thisrow[3*(column+j-1)+2];
	  cout <<"R"<< pix[0] <<"/";
          cout <<"G"<< pix[1]<<"/";
          cout <<"B"<< pix[2]<<" ";
     
  }

  cout << endl;
}
 
}      
                    
